package com.hcrazy.manchester.app.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hcrazy.manchester.app.form.SearchForm;
import com.hcrazy.manchester.app.pojo.LiveSimple;
import com.hcrazy.manchester.app.pojo.LiveSort;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.services.LiveSimpleService;
import com.hcrazy.manchester.app.services.LiveSortService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

@Controller
@RequestMapping("/admin/live")
public class LiveSimpleController {
	
	@Autowired
	private LiveSimpleService liveSimpleService;
	
	@Autowired
	private LiveSortService liveSortService;
	
	/**
	 * 跳转新增页面
	 * 初始化页面数据
	 * @param model
	 */
	@RequestMapping(value="/addSimple",method=RequestMethod.GET)
	public void addSimple(Model model){
		//根据功能码设置分类列表
		setSimpleSorts(model);
		//初始化新增对象
		model.addAttribute("simple", new LiveSimple());
	}
	
	/**
	 * 编辑信息内容
	 * @param infoId 信息标识
	 * @param model 数据返回对象
	 * @return
	 */
	@RequestMapping(value="/editSimple",method=RequestMethod.GET)
	public String editSimple(@RequestParam int infoId,Model model){
		try {
			//初始化对象内容
			addSimple(model);
			//根据id获取信息内容
			LiveSimple simple = liveSimpleService.getInfoById(infoId);
			//更新返回对象
			model.addAttribute("simple",simple);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		return "/admin/live/addSimple";
	}
	
	/**
	 * 提交信息内容
	 * @param simple
	 * @return
	 */
	@RequestMapping(value="/simple",method=RequestMethod.POST)
    public String addOrUpdateSort(@ModelAttribute("form") LiveSimple simple,Model model){
		try {
			liveSimpleService.addOrUpdateLiveSimple(simple);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		return "redirect:/admin/live/live_simple";
    }
	
	/**
	 * 跳转至功能信息列表页
	 * @param model
	 */
	@RequestMapping(value="/live_simple",method=RequestMethod.GET)
	public void live_simple(Model model){
		//根据查询参数获取分页对象：默认第一页、分类参数为0、查询字符串为空
		PageModel pageModel = getAllLiveSimples(1, 0, "");
		model.addAttribute("pageModel", pageModel);
		
		//初始化分类
		setSimpleSorts(model);
		
		//初始化查询对象
		model.addAttribute("searchForm", new SearchForm());
	}
	
	/**
	 * 查询信息列表
	 * @param form 查询参数form
	 * @param pageNum 当前分页
	 * @param model 请求返回对象
	 * @return
	 */
	@RequestMapping(value="/search_simple",method=RequestMethod.POST)
	public String searchSimple(@ModelAttribute("form") SearchForm form,Model model){
		//是否执行删除操作
		if(form.getInfoId() > 0){
			try {
				liveSimpleService.delLiveSimple(form.getInfoId());
			} catch (Exception e) {
				Log.error(e.getMessage(), e);
			}
		}
		//重置infoId
		form.setInfoId(0);
		
		//根据查询参数获取分页对象
		PageModel pageModel = getAllLiveSimples(form.getPageNum(), form.getSortId(), form.getSearch());
		model.addAttribute("pageModel", pageModel);

		//设置查询类别
		setSimpleSorts(model);
		
		//回传查询参数
		model.addAttribute("searchForm",form);
		
		return "/admin/live/live_simple";
	}
	
	/**
	 * 查询全部信息列表
	 * @param pageNum
	 * @param sortId
	 * @param search
	 * @return PageModel
	 */
	private PageModel getAllLiveSimples(int pageNum,int sortId,String search){
		PageModel model = new PageModel();
		model.setPageNum(pageNum);
		try {
			return liveSimpleService.getSimpleInfos(model, search, sortId);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}

	/**
	 * 按功能获取分类并放在在model对象中
	 * @param model
	 */
	private void setSimpleSorts(Model model){
		//根据功能码设置分类列表
		try {
			List<LiveSort> sorts = liveSortService.getLiveSortsByFunCode(Constant.FUN_CODE_SIMPLE);
			model.addAttribute("sorts", sorts);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}		
	}
}
